package com.service.school.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.TDonate;
import com.pageModel.DataGrid;
import com.pageModel.Donate;
import com.service.school.DonateServiceI;

@Service(value = "donateService")
public class DonateServiceImpl implements DonateServiceI {

	private BaseDaoI<TDonate> donateDao;

	public BaseDaoI<TDonate> getDonateDao() {
		return donateDao;
	}
	@Autowired
	public void setDonateDao(BaseDaoI<TDonate> donateDao) {
		this.donateDao = donateDao;
	}
	@Override
	public DataGrid datagrid(Donate donate) {
		DataGrid dg=new DataGrid();
		String hql="from TDonate t ";
		if(donate.getDonor()!=null&&!donate.getDonor().trim().equals("")){
			hql+="where t.donor like '%"+donate.getDonor()+"%'";
		}
		String totalHql = "select count(*) " + hql;
		List<TDonate> l=donateDao.find(hql,donate.getPage(),donate.getRows());
		List<Donate> nl=new ArrayList<Donate>();
		if(l!=null&&l.size()>0){
			for(TDonate t:l){
				Donate u=new Donate();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
		dg.setTotal(donateDao.count(totalHql));
		dg.setRows(nl);
		return dg;
	}
	@Override
	public void remove(String ids) {
		String [] nids=ids.split(",");
		String hql="delete TDonate t where t.donateId in(";
		for(int i=0;i<nids.length;i++){
			if(i>0){
				hql+=",";
			}
			hql+="'"+nids[i]+"'";
		}
		hql+=")";
		donateDao.executeHql(hql);
		
	}
	@Override
	public void add(Donate donate) {
		TDonate t=new TDonate();
		t.setDonateId(UUID.randomUUID().toString());
		t.setDonor(donate.getDonor().trim());
		t.setDonatedItems(donate.getDonatedItems().trim());
		t.setDonateTime(new Date());
		donateDao.saveOrUpdate(t);
	}
	@Override
	public void edit(Donate donate) {
		TDonate t=donateDao.get(TDonate.class,donate.getDonateId());
		BeanUtils.copyProperties(donate, t);
	}

}
